<template>
  <div class="diy_edit gen_edits" id="gen_edit">
    <el-form
      :model="form_subject"
      ref="ruleFormRef"
      label-width="auto"
      class="edit_gen_form"
      size="large"
    >
      <el-row class="row_e">
        <el-col :xs="24" :sm="24" :lg="24" class="el_form_item_warp">
          <el-form-item label="单选题数目" prop="danxuanti_count">
            <el-input-number v-model="danxuanti_count"></el-input-number>
          </el-form-item>
        </el-col>

        <el-col :xs="24" :sm="24" :lg="24" class="el_form_item_warp">
          <el-form-item label="多选题数目" prop="duoxuanti_count">
            <el-input-number v-model="duoxuanti_count"></el-input-number>
          </el-form-item>
        </el-col>

        <el-col :xs="24" :sm="24" :lg="24" class="el_form_item_warp">
          <el-form-item label="判断题数目" prop="panduanti_count">
            <el-input-number v-model="panduanti_count"></el-input-number>
          </el-form-item>
        </el-col>

        <el-col :xs="24" :sm="24" :lg="24" class="el_form_item_warp">
          <el-form-item label="填空题数目" prop="tiankongti_count">
            <el-input-number v-model="tiankongti_count"></el-input-number>
          </el-form-item>
        </el-col>

        <el-col :xs="24" :sm="24" :lg="24" class="el_form_item_warp">
          <el-form-item label="主观题数目" prop="zhuguanti_count">
            <el-input-number v-model="zhuguanti_count"></el-input-number>
          </el-form-item>
        </el-col>
      </el-row>
     
    </el-form>
    <div class="footer">
        <el-button type="primary" @click="handleSubmit()" size="large"
          >提交</el-button
        >
        <el-button @click="cancel()" size="large">取消</el-button>
      </div>
  </div>
</template>

<script setup>
import { ElMessage } from "element-plus";
import { ref, onMounted } from "vue";
import { $post, $get } from "@/api/Api.js";
import storageHelper from "@/libs/StorageHelper";
import { useRouter,useRoute } from "vue-router";
import {
  $delete,
  $fullUrl,
  formatDate,
  $banseUrl,
  $check_action,
  $getUrlParams,
} from "@/utils/utils.js";
let router = useRouter();
let route = useRoute();
let UserInfo = storageHelper.getUserInfo();
let subject_name = route.query.subject_name;
let exam_id = route.query.exam_id;
let url_del = ref("/api/subject_exam_question/del");
let url_exam_question_add = ref("/api/subject_exam_question/add");
let title = ref("subject_exam_question");
let field = ref("exam_id");
let list_subject = ref([]);
let exam_question_list = ref([]);
let danxuanti_count = ref(0);
let duoxuanti_count = ref(0);
let panduanti_count = ref(0);
let tiankongti_count = ref(0);
let zhuguanti_count = ref(0);

let form_subject = ref({
  exam_id: exam_id,
  //  subject_name: subject_name,
});
let ruleFormRef = ref(null);
// 点击取消
let cancel = () => {
  router.go(-1);
};
// 点击提交
let handleSubmit =  () => {
  console.log(exam_id)
  // let valid = await ruleFormRef.value.validate();
  // if (valid) {
 $get("/api/subject_exam_question/del?exam_id="+exam_id).then((res)=>{
if (res.result) {
  // }
    gen_question_list();
    ElMessage.success("提交成功！");
    danxuanti_count.value = 0;
    duoxuanti_count.value = 0;
    panduanti_count.value = 0;
    tiankongti_count.value = 0;
    zhuguanti_count.value = 0;

    router.push("./exams_table");
  } else {
    ElMessage.error(msg);
  }

  })
  
};
//自动生成试题库
let gen_question_list = async () => {
  //题目类型
  let typeArry = [
    { type: "单选题" },
    { type: "多选题" },
    { type: "判断题" },
    { type: "填空题" },
    { type: "主观题" },
  ];
  //题目类型条数
  let sizeArry = [
    danxuanti_count.value,
    duoxuanti_count.value,
    panduanti_count.value,
    tiankongti_count.value,
    zhuguanti_count.value,
  ];
  
  let order = 0;
  for (let i = 0; i < typeArry.length; i++) {
    let query = typeArry[i];
    let size = sizeArry[i];

    //清空数组
    exam_question_list.value.splice(0, exam_question_list.value.length);

    query.subject_name = subject_name;
    //从试卷题库中获取题库列表
    await $get("/api/exam_question_database/get_list", query).then((json) => {
      if (json.result) {
        //打乱顺序
        let list = json.result.list.sort(() => Math.random() - 0.5);

        if (size > list.length) {
          size = list.length;
        }
        for (let j = 0; j < size; j++) {
          order++;
          let exam = list[j];

          // $delete(exam, "update_time");
          // $delete(exam, "create_time");
          // $delete(exam, "exam_question_id");

          exam.exam_id = exam_id;
          exam.subject_name = subject_name;
          exam.question_order = order;

          //添加到试卷题库中
          $post(url_exam_question_add.value, exam).then((json, status) => {});
        }
      } else if (json.error) {
        console.error(json.error);
      }
    });
  }
};
// 获取科目列表
let get_subject_list = async () => {
  let json = await $get("/api/subject/get_list");
  if (json.result) {
    list_subject.value = json.result.list;
  } else if (json.error) {
    console.error(json.error);
  }
};
onMounted(() => {
  get_subject_list();
});
</script>

<style scoped lang="less">
.diy_edit {
  width: 100%;
  .edit_forum_type_form {
    max-width: 60%;
    margin: 90px auto;
    .fn {
      width: 100%;
    }
  }
  img {
    width: 100px;
    height: 100px;
  }
  .edit_list_forum_type_sell {
    width: 80%;
    margin: 90px auto;
  }
  .footer {
    text-align: center;
  }
}
</style>
